#include "beep.h"
#include "delay.h"

int melody[] = {50, 50, 50, 50, 200, 200, 200, 400, 400, 500, 500, 500};


void BEEP_init(void)
{
    GPIO_InitTypeDef GPIO_BEEP;
    /*配置时钟*/
    RCC_APB2PeriphClockCmd(GPIO_BEEP_RCC, ENABLE);
    GPIO_BEEP.GPIO_Mode  = GPIO_Mode_Out_PP;//推挽输出
    GPIO_BEEP.GPIO_Pin 	 = GPIO_BEEP_PIN;
    GPIO_BEEP.GPIO_Speed = GPIO_Speed_50MHz;

    /*配置GPIO 输出*/
    GPIO_Init(GPIO_BEEP_PORT, &GPIO_BEEP);
}

void alarm(void)
{
		BEEP = ~BEEP;
		delay_us(800);
		BEEP = ~BEEP;
		delay_us(800);
}


void Sound(u16 frq)
{
    u32 time;
    if(frq != 1000)
    {
        time = 500000 / ((u32)frq);
        BEEP = 1;
        delay_us(time);
        BEEP = 0;
        delay_us(time);
    }
    else
        delay_us(1000);
}
void Sound2(u16 time)
{
    BEEP = 1;
    delay_ms(time);
    BEEP = 0;
    delay_ms(time);
}

void play_successful(void)
{
    int id = 0;
    for(id = 0 ; id < 12 ; id++)
    {
        Sound2(melody[id]);
    }
}
void play_failed(void)
{
    int id = 0;
    for(id = 11 ; id >= 0 ; id--)
    {
        Sound2(melody[id]);
    }
}
void play_music(unsigned int *tone, unsigned char *music,unsigned char *time)
{
    u32 yanshi;
    u16 i, e;
    yanshi = 10;
    for(i = 0; i < sizeof(music) / sizeof(music[0]); i++)
    {
        for(e = 0; e < ((u16)time[i])*tone[music[i]] / yanshi; e++)
        {
            Sound((u32)tone[music[i]]);
        }
    }
}
